package autoTest.request;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import autoTest.command.ErrorCode;
import autoTest.command.Params;
import autoTest.database.DataManager;
import autoTest.device.Platform;
import autoTest.utils.TextUtils;
import net.sf.json.JSONObject;

public class SetBlackListPart extends RequestPart {
	private static final long serialVersionUID = 2271797150647771301L;
	
//	private static Map<String, Set<String>> blackListDir = new ConcurrentHashMap<String, Set<String>>();
//	private static Map<String, Set<String>> blackListCaseId = new ConcurrentHashMap<String, Set<String>>();
	
	public static Set<String> getBlackListDirByPlatform(Platform platform) {
		return DataManager.getBlackList(platform, 0);	//blackListDir.get(platform.valueStr());
	}
	
	public static Set<String> getBlackListCaseByPlatform(Platform platform) {
		return DataManager.getBlackList(platform, 1);	//blackListCaseId.get(platform.valueStr());
	}
	
	protected void doResponse(HttpServletResponse resp, int code) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");  
		resp.setContentType("text/plain");
		resp.setStatus(HttpServletResponse.SC_OK);
		PrintWriter pWriter = resp.getWriter();
		JSONObject joRet = new JSONObject();
		joRet.put(Params.CODE, code);
		pWriter.println(joRet.toString());  
	}

	@Override protected void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	String platformStr = req.getParameter(Params.PLAT_FORM);
    	String enableStr = req.getParameter(Params.ENABLE);
    	String typeStr = req.getParameter(Params.TYPE);
    	String contentStr = req.getParameter(Params.CONTENT);
    	
    	System.out.println("[doResponse] platformStr:" + platformStr + ", typeStr:" + typeStr + ", enableStr:" + enableStr + ", contentStr:" + contentStr);
    	
    	Platform platform = Platform.valueStrOf(platformStr);
    	if (platform == null) {
    		doResponse(resp, ErrorCode.ERR_PARAM_ERROR);
    		return;
    	}
    	
    	int typeValue = -1;
    	try {
    		typeValue = Integer.valueOf(typeStr);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	
    	int enableValue = -1;
    	try {
    		enableValue = Integer.valueOf(enableStr);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	
    	if (!(enableValue == 0 || enableValue == 1)) {
    		doResponse(resp, ErrorCode.ERR_PARAM_ERROR);
    		return;
    	}
    	
		String[] contentEles = contentStr.split(",");
		List<String> allEles = new ArrayList<String>();
		for (String ele : contentEles) {			//筛选空数据
			if (!TextUtils.isEmpty(ele)) {
				allEles.add(ele);
			}
		}
		if (allEles.size() <= 0) {
			doResponse(resp, ErrorCode.ERR_NO_TESTCASE);
			return;
		}
    	
    	if (typeValue == 0) {	//使用路径筛选
			Set<String> tmp = DataManager.getBlackList(platform, typeValue);	//blackListDir.get(platformStr);
			if (tmp == null) {
				tmp = new HashSet<String>();
			}
			
    		if (enableValue == 0) {			//用例无效
    			tmp.addAll(allEles);
    		} else {
    			if (!tmp.removeAll(allEles)) {
    				doResponse(resp, ErrorCode.ERR_NO_TESTCASE);
    				return;
    			}
    		}
    		DataManager.putBlackList(platform, typeValue, tmp);	//    		blackListDir.put(platformStr, tmp);

    	} else if (typeValue == 1) {	//使用编号筛选
    		Set<String> tmp = DataManager.getBlackList(platform, typeValue);	//blackListCaseId.get(platformStr);
			if (tmp == null) {
				tmp = new HashSet<String>();
			}
    		if (enableValue == 0) {
    			tmp.addAll(allEles);
    		} else {
    			if (!tmp.removeAll(allEles)) {
    				doResponse(resp, ErrorCode.ERR_NO_TESTCASE);
    				return;
    			}
    		}
    		DataManager.putBlackList(platform, typeValue, tmp);		//blackListCaseId.put(platformStr, tmp);
    	} else if (typeValue == 2) {	//全部路径，以及用例
    		if (enableValue == 1) {
    			DataManager.removeBlackListByInfo(platform, 0);		//blackListCaseId.put(platformStr, new HashSet<String>());
    			DataManager.removeBlackListByInfo(platform, 1);		//blackListDir.put(platformStr, new HashSet<String>());
    		}
    	} else {
    		doResponse(resp, ErrorCode.ERR_PARAM_ERROR);
    		return;
    	}
    	doResponse(resp, ErrorCode.SUCCESS);
	}
}
